package com.ccut;

import com.ccut.Constant.EllipseOrientation;
import com.ccut.Constant.ShapeType;

import static com.ccut.Constant.EllipseOrientation.HORIZONTAL;
import static com.ccut.Constant.EllipseOrientation.VERTICAL;

public class ShapeView {

	private final ShapeList shapeList = new ShapeList();

	public ShapeView() {
	}

	public void enterMainMenu() {
		boolean loopFlag = true;
		do {
			System.out.println("\n----------------图形周长、面积计算、统计展示-----------------\n");
			System.out.println("                   1 添 加 图 形");
			System.out.println("                   2 查 看 所 有 图 形 信 息");
			System.out.println("                   3 查 看 所 有 图 形 周 长 总 计");
			System.out.println("                   4 查 看 所 有 图 形 面 积 总 计");
			System.out.println("                   5 显 示 所 有 图 形");
			System.out.println("                   6 退       出\n");
			System.out.print("                   请选择(1-6)：");

			char key = CMUtility.readMenuSelection();
			System.out.println();
			switch (key) {
			case '1':
				addNewShape();
				break;
			case '2':
				listAllShapesInfo();
				break;
			case '3':
				listShapeTotalParameter();
				break;
			case '4':
				listShapeTotalArea();
				break;
			case '5':
				listAllShapes();
				break;
			case '6':
				System.out.print("确认是否退出(Y/N)：");
				char yn = CMUtility.readConfirmSelection();
				if (yn == 'Y')
					loopFlag = false;
				break;
			}
		} while (loopFlag);
	}

	private void listAllShapes() {
		System.out.println("--------------------所有图形--------------------");
		shapeList.showShape();
		System.out.println("--------------------------------------------------");
	}

	private void listShapeTotalArea() {
		System.out.println("-----------------图形面积统计-----------------");
		shapeList.showShapeTotalArea();
		System.out.println("--------------------------------------------------");
	}

	private void listShapeTotalParameter() {
		System.out.println("-----------------图形周长统计-----------------");
		shapeList.showShapeTotalParameter();
		System.out.println("--------------------------------------------------");
	}

	private void listAllShapesInfo() {
		System.out.println("--------------------所有图形--------------------");
		shapeList.showShapeInfo();

		System.out.println("--------------------------------------------------");
	}

	private void addNewShape() {
		System.out.println("---------------------添加图形---------------------");
		System.out.println("请选择图形的类型:");

		// 动态遍历枚举类型显示选项（显示中文名称）
		ShapeType[] shapeTypes = ShapeType.values();
		for (int i = 0; i < shapeTypes.length; i++) {
			System.out.println((i + 1) + ". " + shapeTypes[i].getChineseName() + "(" + shapeTypes[i].name() + ")");
		}
		System.out.print("请选择(1-" + shapeTypes.length + "): ");

		// 读取用户选择
		int choice = CMUtility.readInt();

		// 验证用户输入
		if (choice < 1 || choice > shapeTypes.length) {
			System.out.println("无效的选择！");
			return;
		}

		// 获取选择的图形类型
		ShapeType selectedShapeType = shapeTypes[choice - 1];
		System.out.println("您选择了: " + selectedShapeType.getChineseName());

		// 根据选择的类型创建相应的图形对象
		switch (selectedShapeType) {
			case CIRCLE:
				System.out.print("请输入半径：");
                new Circle(CMUtility.readDouble());
                break;
			case RECTANGLE:
				System.out.print("请输入长：");
				double length = CMUtility.readDouble();
				System.out.print("请输入宽：");
				double width = CMUtility.readDouble();
				new Parallelogram(length, width,90);
				break;
			case SQUARE:
				System.out.print("请输入边长：");
				new Parallelogram(CMUtility.readDouble(),90);
				break;
			case TRIANGLE:
				System.out.print("请输入第一条边：");
				double side1 = CMUtility.readDouble();
				System.out.print("请输入第二条边：");
				double side2 = CMUtility.readDouble();
				System.out.print("请输入第三条边：");
				double side3 = CMUtility.readDouble();
				new Trangle(side1,side2,side3);
				break;
			case RHOMBUS:
				System.out.print("请输入边长：");
				side1 = CMUtility.readDouble();
				System.out.print("请输入任意相邻两边的角度：");
				double angle = CMUtility.readDouble();
				new Parallelogram(side1,angle);
				break;
			case ELLIPSE:
				System.out.print("请输入长半轴：");
				double longAxis = CMUtility.readDouble();
				System.out.print("请输入短半轴：");
				double shortAxis = CMUtility.readDouble();
				System.out.print("请输入长半轴所在的轴，X轴输入0，Y轴输入1:");
				EllipseOrientation axis = CMUtility.readInt() == 0  ? HORIZONTAL : VERTICAL;
				new Ellipse(longAxis,shortAxis,axis);
				break;
			case TRAPEZOID:
				System.out.print("请输入上底：");
				double upperBase = CMUtility.readDouble();
				System.out.print("请输入下底：");
				double lowerBase = CMUtility.readDouble();
				System.out.print("请输入高：");
				double height = CMUtility.readDouble();
				new Trapezoid(upperBase,lowerBase,height);
				break;
			case PARALLELOGRAM:
				System.out.print("请输入第一条边：");
				side1 = CMUtility.readDouble();
				System.out.print("请输入第二条边：");
				side2 = CMUtility.readDouble();
				System.out.print("请输入任意相邻两边的角度：");
				angle = CMUtility.readDouble();
				new Parallelogram(side1,side2,angle);
				break;
		}
		System.out.println("---------------------添加完成---------------------");
	}

	public static void main(String[] args) {
		ShapeView cView = new ShapeView();
		cView.enterMainMenu();
	}
}
